package com.novoda.runbuddy.view;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.novoda.runbuddy.controller.IStopClockController;

public class TargetTimeView extends LinearLayout {
	private TextView				hours;
	private TextView				minutes;
	private TextView				seconds;
	private TextView				centiSecs;
	private Button					hoursUp;
	private Button					hoursDown;
	private TextView				hoursSep;
	private Button					minutesUp;
	private Button					minutesDown;
	private TextView				minutesSep;
	private Button					secondsUp;
	private Button					secondsDown;
	private TextView				secondsSep;
	private Button					milliSecondsUp;
	private Button					milliSecondsDown;
	private CheckBox				targetChecked;
	private IStopClockController	stopwatchController;

	public TargetTimeView(Context context, CheckBox targetChecked, TextView hours, TextView minutes, TextView seconds,
			TextView millisecs, Button hoursUp, Button hoursDown, TextView hoursSep, Button minutesUp, Button minutesDown,
			TextView minutesSep, Button secondsUp, Button secondsDown, TextView secondsSep, Button milliSecondsUp,
			Button milliSecondsDown, IStopClockController stopwatchController) {
		super(context);
		this.targetChecked = targetChecked;
		this.hours = hours;
		this.minutes = minutes;
		this.seconds = seconds;
		this.centiSecs = millisecs;
		this.hoursUp = hoursUp;
		this.hoursDown = hoursDown;
		this.hoursSep = hoursSep;
		this.minutesUp = minutesUp;
		this.minutesDown = minutesDown;
		this.minutesSep = minutesSep;
		this.secondsUp = secondsUp;
		this.secondsDown = secondsDown;
		this.secondsSep = secondsSep;
		this.milliSecondsUp = milliSecondsUp;
		this.milliSecondsDown = milliSecondsDown;

		this.stopwatchController = stopwatchController;

		hours.setText("00");
		minutes.setText("00");
		seconds.setText("00");
		millisecs.setText("00");

		targetChecked.setChecked(false);
		targetChecked.setOnClickListener(targetIsVisible);
		hoursUp.setOnClickListener(hoursUpListener);
		hoursDown.setOnClickListener(hoursDownListener);
		minutesUp.setOnClickListener(minutesUpListener);
		minutesDown.setOnClickListener(minutesDownListener);
		secondsUp.setOnClickListener(secondsUpListener);
		secondsDown.setOnClickListener(secondsDownListener);
		milliSecondsUp.setOnClickListener(centiSecsUpListener);
		milliSecondsDown.setOnClickListener(centiSecsDownListener);

	}

	public void hideSetTargetTime() {
		hours.setVisibility(View.INVISIBLE);
		minutes.setVisibility(View.INVISIBLE);
		seconds.setVisibility(View.INVISIBLE);
		centiSecs.setVisibility(View.INVISIBLE);
		hoursUp.setVisibility(View.INVISIBLE);
		hoursDown.setVisibility(View.INVISIBLE);
		hoursSep.setVisibility(View.INVISIBLE);
		minutesUp.setVisibility(View.INVISIBLE);
		minutesDown.setVisibility(View.INVISIBLE);
		minutesSep.setVisibility(View.INVISIBLE);
		secondsUp.setVisibility(View.INVISIBLE);
		secondsDown.setVisibility(View.INVISIBLE);
		secondsSep.setVisibility(View.INVISIBLE);
		milliSecondsUp.setVisibility(View.INVISIBLE);
		milliSecondsDown.setVisibility(View.INVISIBLE);
	}

	public void turnOnTarget() {
		hours.setVisibility(View.VISIBLE);
		minutes.setVisibility(View.VISIBLE);
		seconds.setVisibility(View.VISIBLE);
		centiSecs.setVisibility(View.VISIBLE);
		hoursUp.setVisibility(View.VISIBLE);
		hoursDown.setVisibility(View.VISIBLE);
		hoursSep.setVisibility(View.VISIBLE);
		minutesUp.setVisibility(View.VISIBLE);
		minutesDown.setVisibility(View.VISIBLE);
		minutesSep.setVisibility(View.VISIBLE);
		secondsUp.setVisibility(View.VISIBLE);
		secondsDown.setVisibility(View.VISIBLE);
		secondsSep.setVisibility(View.VISIBLE);
		milliSecondsUp.setVisibility(View.VISIBLE);
		milliSecondsDown.setVisibility(View.VISIBLE);
	}

	private String increaseByOne(TextView textView) {
		Integer currentValue = Integer.parseInt(textView.getText().toString());
		currentValue++;

		if (currentValue < 10) {
			return "0" + currentValue.toString();
		} else {
			return currentValue.toString();
		}

	}

	private String decreaseByOne(TextView textView) {
		Integer currentHourValue = Integer.parseInt(textView.getText().toString());
		currentHourValue--;
		String hourValue;

		if (currentHourValue < 0) {
			return "00";
		}

		if (currentHourValue < 10) {
			hourValue = "0" + currentHourValue.toString();
		} else {
			hourValue = currentHourValue.toString();
		}

		return hourValue;
	}

	private OnClickListener	hoursUpListener			= new OnClickListener()
													{
														public void onClick(View v) {
															hours.setText(increaseByOne(hours));
															stopwatchController.setTargetHours(Integer.parseInt(hours.getText()
																	.toString()));
														}
													};
	private OnClickListener	hoursDownListener		= new OnClickListener()
													{
														public void onClick(View v) {
															hours.setText(decreaseByOne(hours));
															stopwatchController.setTargetHours(Integer.parseInt(hours.getText()
																	.toString()));
														}
													};
	private OnClickListener	minutesUpListener		= new OnClickListener()
													{
														public void onClick(View v) {
															minutes.setText(increaseByOne(minutes));
															stopwatchController.setTargetMins(Integer.parseInt(minutes.getText()
																	.toString()));
														}
													};

	private OnClickListener	minutesDownListener		= new OnClickListener()
													{
														public void onClick(View v) {
															minutes.setText(decreaseByOne(minutes));
															stopwatchController.setTargetMins(Integer.parseInt(minutes.getText()
																	.toString()));
														}
													};

	private OnClickListener	secondsUpListener		= new OnClickListener()
													{
														public void onClick(View v) {
															seconds.setText(increaseByOne(seconds));
															stopwatchController.setTargetSecs(Integer.parseInt(seconds.getText()
																	.toString()));
														}
													};

	private OnClickListener	secondsDownListener		= new OnClickListener()
													{
														public void onClick(View v) {
															seconds.setText(decreaseByOne(seconds));
															stopwatchController.setTargetSecs(Integer.parseInt(seconds.getText()
																	.toString()));
														}
													};

	private OnClickListener	centiSecsUpListener		= new OnClickListener()
													{
														public void onClick(View v) {
															centiSecs.setText(increaseByOne(centiSecs));
															stopwatchController.setTargetCentiSecs(Integer.parseInt(centiSecs
																	.getText().toString()));
														}
													};

	private OnClickListener	centiSecsDownListener	= new OnClickListener()
													{
														public void onClick(View v) {
															centiSecs.setText(decreaseByOne(centiSecs));
															stopwatchController.setTargetCentiSecs(Integer.parseInt(centiSecs
																	.getText().toString()));
														}
													};

	OnClickListener			targetIsVisible			= new OnClickListener()
													{
														public void onClick(View v) {
															if (targetChecked.isChecked()) {
																turnOnTarget();
																stopwatchController.turnOnASetTargetTime();
															} else {
																hideSetTargetTime();
																stopwatchController.turnOffASetTargetTime();
															}
														}
													};

}
